package com.javasm.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Properties;

@RestController
public class AuthController {

    @RequestMapping("add")
    @PreAuthorize("hasAuthority('add')")
    public String add(){
        return "add方法";
    }

    @RequestMapping("del")
    @PreAuthorize("hasAuthority('del')")
    public String del(){
        return "del方法";
    }

    @RequestMapping("query")
    @PreAuthorize("hasAuthority('query')")
    public String query(){
        return "query方法";
    }

    @RequestMapping("update")
    @PreAuthorize("hasAuthority('update')")
    public String update(){
        return "update方法";
    }


    @RequestMapping("phone/captcha")
    public String phoneCaptcha(HttpSession session){
        Properties properties=new Properties();
        properties.setProperty("kaptcha.image.width","150");
        properties.setProperty("kaptcha.image.height","50");
        properties.setProperty("kaptcha.textproducer.char.string","0123456789");
        properties.setProperty("kaptcha.textproducer.char.length","4");
        Config config=new Config(properties);
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(config);
        String captcha = kaptcha.createText();
        session.setAttribute("phoneCaptcha",captcha);
        return captcha;
    }
}
